package com.mofang.powerdekorhelper.utils;

import com.mofang.powerdekorhelper.model.OrderDetial;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by luofei on 2017/8/25 0025.
 */

public class ParamsUtils {

    public static String getOrderListParams(int page, int pageSize, boolean ispage, int sellerId, Integer ordreState, String search) {
        String jsonParams = "";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("page", page);
            jsonObject.put("pageSize", pageSize);
            jsonObject.put("ispage", ispage);

            JSONObject param = new JSONObject();

            param.put("sellerId", sellerId);

            if (ordreState != null && ordreState != 0) {
                param.put("orderState", ordreState);
            }

            if (!"".equals(search)) {
                param.put("search", search);
            }

            jsonObject.put("param", param);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonParams = jsonObject.toString();

        return jsonParams;
    }

    public static String getOrderListParams(Integer orderId) {
        String jsonParams = "";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("orderId", orderId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonParams = jsonObject.toString();
        return jsonParams;
    }

    public static String getCommitOrderParams(String orderCode, Integer shopId, Integer sellerId, double orderPrice, Integer customerId,
                                              String consignee, String consigneePhone, String consigneeAddress, double realPayPrice, String remarks,
                                              double depositPrice, String isUseActivityCard, String payStatus, String prize, Integer activityId,
                                              Integer typistId, List<OrderDetial.ProductInfoList> productInfoLists, List<OrderDetial.Num> nums,
                                              String createTime, Integer explainId, List<OrderDetial.OrderGiving> orderGivingList) {
        String jsonParams = "";

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("orderPrice", orderPrice);
            jsonObject.put("orderCode", orderCode);
            jsonObject.put("shopId", shopId);
            jsonObject.put("sellerId", sellerId);
            jsonObject.put("customerId", customerId);
            jsonObject.put("consignee", consignee);
            jsonObject.put("consigneePhone", consigneePhone);
            jsonObject.put("consigneeAddress", consigneeAddress);
            jsonObject.put("realPayPrice", realPayPrice);
            jsonObject.put("remarks", remarks);
            jsonObject.put("depositPrice", depositPrice);
            jsonObject.put("isUseActivityCard", isUseActivityCard);
            jsonObject.put("payStatus", payStatus);
            jsonObject.put("prize", prize);
            jsonObject.put("activityId", activityId);
            jsonObject.put("typistId", typistId);
            jsonObject.put("createTime", createTime);
            jsonObject.put("explainId", explainId);

            // 产品
            JSONArray productArray = new JSONArray();

            for (OrderDetial.ProductInfoList product : productInfoLists) {

                JSONObject JproductObject = new JSONObject();

                JproductObject.put("imageUrl", product.getImageUrl());
                JproductObject.put("productModel", product.getProductModel());
                JproductObject.put("num", product.getNum());
                JproductObject.put("price", product.getPrice());
                JproductObject.put("productName", product.getProductName());
                JproductObject.put("totalPrice", product.getTotalPrice());

                productArray.put(JproductObject);
            }

            jsonObject.put("productInfoList", productArray);

            // 奖项
            if (nums != null) {
                JSONArray jsonArray = new JSONArray();
                for (OrderDetial.Num num : nums) {
                    JSONObject prizes = new JSONObject();
                    prizes.put("itemId", num.getItemId());
                    prizes.put("num", num.getNum());
                    jsonArray.put(prizes);
                }
                jsonObject.put("num", jsonArray);
            }

            // orderGiving
            if (orderGivingList != null) {
                JSONArray orderGivingArray = new JSONArray();
                for (int i = 0; i < orderGivingList.size(); i++) {
                    JSONObject orderGivingObject = new JSONObject();
                    orderGivingObject.put("activityId",activityId);
                    orderGivingObject.put("giftId",orderGivingList.get(i).getGiftId());
                    orderGivingObject.put("gift",orderGivingList.get(i).getGift());
                    orderGivingArray.put(orderGivingObject);
                }
                jsonObject.put("orderGiving",orderGivingArray);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonParams = jsonObject.toString();
        return jsonParams;
    }

    public static String getEditOrderParams(Integer orderId,
                                            String consignee, String consigneePhone, String consigneeAddress, String province, String city,
                                            String district, String realPayPrice, String remarks, String depositPrice, String isUseActivityCard,
                                            String payStatus, String prize,
                                            List<OrderDetial.ProductInfoList> productInfoLists, JSONArray jsonArray) {
        String jsonParams = "";

        double orderPrice = 0;
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("orderId", orderId);
            jsonObject.put("consignee", consignee);
            jsonObject.put("consigneePhone", consigneePhone);
            jsonObject.put("consigneeAddress", consigneeAddress);
            jsonObject.put("province", province);
            jsonObject.put("city", city);
            jsonObject.put("district", district);
            jsonObject.put("realPayPrice", realPayPrice);
            jsonObject.put("remarks", remarks);
            jsonObject.put("depositPrice", depositPrice);
            jsonObject.put("isUseActivityCard", isUseActivityCard);
            jsonObject.put("payStatus", payStatus);
            jsonObject.put("prize", prize);

            // 产品
            JSONArray productArray = new JSONArray();

            for (OrderDetial.ProductInfoList product : productInfoLists) {

                JSONObject JproductObject = new JSONObject();

                JproductObject.put("imageUrl", product.getImageUrl());
                JproductObject.put("productModel", product.getProductModel());
                JproductObject.put("num", product.getNum());
                JproductObject.put("price", product.getPrice());
                JproductObject.put("productName", product.getProductName());
                JproductObject.put("totalPrice", product.getTotalPrice());

                productArray.put(JproductObject);

                orderPrice = orderPrice + product.getTotalPrice();
            }
            jsonObject.put("productInfoList", productArray);

            //订单总价
            jsonObject.put("orderPrice", orderPrice);

            // 奖项
            jsonObject.put("num", jsonArray);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonParams = jsonObject.toString();
        return jsonParams;
    }


    public static String getCloseOrder(int orderId, String closeCause) {

        String jsonParams = "";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("orderId", orderId);
            jsonObject.put("closeCause", closeCause);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();

        return jsonParams;
    }

    // 客户列表
    public static String getCustomerListParams(Integer page, Integer pageSize, boolean ispage, Integer sellerId, String search, Integer property, String customerStatus) {

        String jsonParams = "";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("page", page);
            jsonObject.put("pageSize", pageSize);
            jsonObject.put("ispage", ispage);

            JSONObject params = new JSONObject();
            params.put("sellerId", sellerId);

            if (!search.equals("")) {
                params.put("search", search);
            }
            if (property != 0) {
                params.put("property", property);
            }
            if (!customerStatus.equals("")) {
                params.put("customerStatus", customerStatus);
            }
            jsonObject.put("param", params);

            jsonParams = jsonObject.toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jsonParams;
    }

    public static String getCustomerActivityDetailParams(Integer activityId, Integer customerId) {
        String jsonParams = "";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", activityId);
            jsonObject.put("customer_id", customerId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonParams = jsonObject.toString();

        return jsonParams;
    }

    public static String getcheckOrderParams(String orderCode, Integer activityId, Integer shopId, Integer customerId) {
        String jsonParams = "";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("orderCode", orderCode);
            jsonObject.put("activityId", activityId);
            jsonObject.put("shopId", shopId);
            jsonObject.put("customerId", customerId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonParams = jsonObject.toString();

        return jsonParams;
    }

    public static String getManagerTaskParams(Integer userId, Integer page, Integer pageSize, Integer state) {

        String jsonParams = "";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("page", page);
            jsonObject.put("pageSize", pageSize);

            JSONObject param = new JSONObject();
            param.put("user_manager_id", userId);
            if (state != null) {
                param.put("state", state);
            }
            jsonObject.put("param", param);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();

        return jsonParams;
    }


    public static String getRefreshOrderParams(Integer orderId, String orderCode, Integer activityId, Integer customerId) {

        String jsonParams = "";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("orderId", orderId);
            jsonObject.put("orderCode", orderCode);
            jsonObject.put("activityId", activityId);
            jsonObject.put("customerId", customerId);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();

        return jsonParams;
    }

    public static String getAddCommunicateParams(Integer customerId, Integer userManagerId, String communicate_time_start, Integer gender, String age, double erection_space, double floor_space,
                                                 String installation_time_start, String installation_time_end, String install_style,
                                                 String communicate_content, String state) {

        String jsonParams = "";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("customer_id", customerId);
            jsonObject.put("userManager_id", userManagerId);

            long timeStart = DateUtils.getTimeStr(communicate_time_start);

            if (!installation_time_start.equals("未定")) {
                long ctimeStart = DateUtils.getTimeStr(installation_time_start);
                jsonObject.put("installation_time_start", ctimeStart);
            }

            if (!installation_time_end.equals("未定")) {
                long ctimeEnd = DateUtils.getTimeStr(installation_time_end);
                jsonObject.put("installation_time_end", ctimeEnd);
            }

            jsonObject.put("communicate_time_start", timeStart);
            jsonObject.put("gender", gender);
            jsonObject.put("age", age);
            jsonObject.put("erection_space", erection_space);
            jsonObject.put("floor_space", floor_space);

            jsonObject.put("install_style", install_style);

            JSONObject customerUserManagerBasic = new JSONObject();

            Integer stateFlag = null;

            if (state.equals("已联系")) {
                stateFlag = 1;
            } else if (state.equals("已进店")) {
                stateFlag = 2;
            } else if (state.equals("已购卡")) {
                stateFlag = 3;
            } else if (state.equals("已订单")) {
                stateFlag = 4;
            } else if (state.equals("已活动")) {
                stateFlag = 5;
            } else if (state.equals("已物流")) {
                stateFlag = 6;
            } else if (state.equals("已安装")) {
                stateFlag = 7;
            } else if (state.equals("售后中")) {
                stateFlag = 8;
            } else if (state.equals("已关闭")) {
                stateFlag = 9;
            }

            customerUserManagerBasic.put("state", stateFlag);

            customerUserManagerBasic.put("communicate_content", communicate_content);
            jsonObject.put("customerUserManagerBasic", customerUserManagerBasic);
            customerUserManagerBasic.put("state", stateFlag);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();

        return jsonParams;
    }


    // 4、修改订单参数 updateOrder
    public static String getUpdateOrderParams(Integer orderId, String remarks, String isUseActivityCard) {

        String jsonParams = "";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("orderId", orderId);
            jsonObject.put("remarks", remarks);
            jsonObject.put("isUseActivityCard", isUseActivityCard);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();

        return jsonParams;
    }
}
